package com.mapbased.cc.valid;

import com.mapbased.wfw.*;
import java.io.UnsupportedEncodingException;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author chy_hs(Baily)
 * @version 1.0
 */
public class LoginName
    implements Validator
{
    /**
     *
     * @param value String
     * @param rc RenderContext
     * @return boolean
     * @todo Implement this com.mapbased.cc.valid.Validator method
     */
    public boolean isValid(String v,RenderContext rc)
    {
        return verifyLoginName(v);
    }

    public String toString()
    {
        return "格式不正确!";
    }

    public static boolean verifyLoginName(String ln)
    {
        if(ln.length() > 20 || ln.length() < 3)
        {
            return false;
        }
        try
        {

            if(!ln.equals(java.net.URLEncoder.encode(ln,"utf-8")))
            {
                return false;
            }
        }
        catch(UnsupportedEncodingException ex)
        {
            return false;
        }

        if(!Character.isLetter(ln.charAt(0)))
        {
            return false;
        }
        for(int i=0;i < ln.length();i++)
        {
            char c=ln.charAt(i);
            if(!Character.isLetterOrDigit(c))
            {
                if(c != '_' && c != '.' && c != '-')
                {
                    return false;
                }
            }
        }

        return true;

    }

}
